﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHNOPData.SHNOPModel;
using SHNOPData.common;

namespace SHNOPData.ImportExcel
{
    public class ImportNtPlan : IImportExcel
    {
        public void Import(string[,] array)
        {
            string retMsg = "";
            string errorMsg = "";
            int successCount = 0;
        
            List<NTPlanBase> listZd = GetNtPlanBaseList(array, out errorMsg);
           
            int failCount = array.GetLength(0) - listZd.Count-1;
          
            foreach (var item in listZd)
            {
                //bool isValidId = IsValidPlanID(item, ref retMsg);
                //if (!isValidId)
                //{
                //    failCount++;
                //    continue;
                //}
                bool result = AppDBMng.ImportNtPlan(item);
                if (result)
                {
                    successCount += 1;
                }
                else
                {
                    failCount++;
                    retMsg = retMsg + "第" + item.No + "条记录导入失败\n";
                }
            }

            if (failCount == 0)
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条记录";
            }
            else
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条,失败" + failCount.ToString() + "条\n" + retMsg;
            }
           
        }
        private bool IsValidPlanID(NTPlanBase item, ref string retMsg)
        {
            //if (item.GuihuaID2G.Length > 0)
            //{
            //    item.GuihuaIDTD = null;
            //    return true;
            //}
            //else if (item.GuihuaID2G.Length == 0 && item.GuihuaIDTD.Length == 0)
            //{
            //    retMsg = retMsg + "第" + item.No + "条记录导入失败,规划编号必须至少填写一个\n";
            //}
            return false;
        }
        private  int? Convert2Int(string info)
        {
            int outResult = 0;
            int? valOrNull = (info.Length != 0 && int.TryParse(info, out outResult)) ? outResult : (int?)null;
            return valOrNull;
        }
        private float? Convert2Float(string info)
        {
            float outResult = 0;
            float? valOrNull = (info.Length != 0 && float.TryParse(info, out outResult)) ? outResult : (float?)null;
            return valOrNull;
        }
        public List<NTPlanBase> GetNtPlanBaseList(string[,] array, out string msg)
        {
            msg = "";
            List<NTPlanBase> ntPlanList = new List<NTPlanBase>();
            if (array != null)
            {
                for (int i = 1; i < array.GetLength(0); i++)
                {
                    int j = 0;
                    while (j < array.GetLength(1))
                    {
                        try
                        {
                            NTPlanBase ntPlan = new NTPlanBase();
                            ntPlan.No = array[i, j++];
                            if (ntPlan.No.Length == 36)
                            {
                                ntPlan.ntplanID = new Guid(ntPlan.No);
                            }
                            #region 规划信息
                            ntPlan.jiaZhanPro = array[i, j++];
                            ntPlan.CompanyName= array[i, j++];
                            ntPlan.belongScheme = array[i, j++];
                            ntPlan.TDBelongScheme = array[i, j++];
                            ntPlan.XieTongNum = array[i, j++];
                            ntPlan.isTiaoZheng = array[i, j++];
                            ntPlan.isComplaint = array[i, j++];
                            ntPlan.planningNum = array[i, j++];
                            ntPlan.TDPlanningNum = array[i, j++];
                            ntPlan.longitude = Convert2Float(array[i, j++]);
                            ntPlan.latitude = Convert2Float(array[i, j++]);
                            ntPlan.HuanXianInfo = array[i, j++];
                            ntPlan.DeviceFacturer = array[i, j++];

                            ntPlan.Channel = array[i, j++];
                            ntPlan._900PlanPeiZhi = array[i, j++];
                            ntPlan._1800PlanPeiZhi = array[i, j++];
                            ///根据环线信息和配置算出端口
                            ntPlan.TDPlanPeiZhi = array[i, j++];
                            ntPlan.TDPeiZhi = Convert2Int(array[i, j++]);
                            ntPlan.TDPlanDuanKou = Convert2Int(array[i, j++]);
                            ntPlan.JiaZhanType = array[i, j++];
                            ntPlan.jiaZhanAim=array[i, j++]; ;
                            #endregion
                            ntPlanList.Add(ntPlan);
                        }
                        catch (System.Exception ex)
                        {
                        	//msg
                        }
                        
                    }

                }
            }
            return ntPlanList;
        }
    }
}
